package com.yingpai.fitness.dvp_2_0.main.activity.vip;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.DateFormatUtil;
import com.yingpai.fitness.dvp_2_0.common.util.SharedPreferencesHelp;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.vip_card.MemberInfoBean;
import com.yingpai.fitness.entity.wx.TokenBean;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ASUS on 2018/8/23.
 */

public class QR_Activity extends CommonActivity {

    @BindView(R.id.qr_image)
    ImageView qrImage;
    @BindView(R.id.title_bar)
    RelativeLayout title_bar;
    @BindView(R.id.expireDate)
    TextView mtv_expireDate;
    private String qr_data;
    private TokenBean tokenBean;
    private Handler handler;
    private Runnable runnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.activity_qr);
    }

    @Override
    protected void initView() {

        tokenBean = (TokenBean) SharedPreferencesHelp.getBean(Constant.TOKENBEAN);
        handler = new Handler();
        setTitle("开门码");
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            qr_data = bundle.getString("qr_data");
        }

        qrImage.post(new Runnable() {
            @Override
            public void run() {
                qrImage.setImageBitmap(CodeUtils.createImage(qr_data, qrImage.getWidth(), qrImage.getWidth(), BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)));
            }
        });

    }

    @Override
    protected void initData() {
        refreshQr();
    }

    private void refreshQr() {
        runnable = new Runnable() {
            @Override
            public void run() {
                HttpParams httpParams = new HttpParams();
                httpParams.put("customerId", tokenBean.getId());
                finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.GET_INFORMATION, httpParams, Url.GET_INFORMATION, new Callback<MemberInfoBean>() {
                    @Override
                    public void onSuccess(MemberInfoBean memberInfoBean) {
                        if (Constant.REQUEST_SUCCESS.equals(memberInfoBean.getResult())) {
                            JSONObject jsonObject = new JSONObject();
                            try {
                                jsonObject.put("customerId", tokenBean.getId());
                                jsonObject.put("createTime", DateFormatUtil.getTodaydate());
                                jsonObject.put("activeTime", 10);
                                jsonObject.put("QRCodeId", memberInfoBean.getMap().getQRCodeId());
                                jsonObject.put("expireDate", memberInfoBean.getMap().getCustomer().getExpireDate());
                                qrImage.setImageBitmap(CodeUtils.createImage(jsonObject.toString(), qrImage.getWidth(), qrImage.getWidth(), BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)));
                                handler.postDelayed(runnable,10000);
                                mtv_expireDate.setText("会员有效期至【"+memberInfoBean.getMap().getCustomer().getExpireDate()+"】");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            showToast(memberInfoBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }
                });
            }
        };
        //handler.postDelayed(runnable,10000);
        handler.post(runnable);
    }

    @Override
    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.back:
                finish_refesh();
                break;
        }
    }

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        finish_refesh();
    }

    private void finish_refesh() {
        handler.removeCallbacks(runnable);
        EventBus.getDefault().post(Constant.REQUEST_SUCCESS);
        finishActivity();
    }
}
